﻿namespace TestTool.GUI.Controls.Device
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Net;
    using System.Windows.Forms;
    using TestTool.GUI;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Controls;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Utils;
    using TestTool.GUI.Views;

    internal class DeviceManagementPage : Page, IDeviceManagementView, IView
    {
        private DeviceManagementController _controller;
        private Button btnDeviceInfo;
        private Button btnGetCapabilities;
        private Button btnGetHostname;
        private Button btnGetInterfaces;
        private Button btnGetServices;
        private Button btnHardReset;
        private Button btnProbe;
        private Button btnReboot;
        private Button btnSetGateway;
        private Button btnSetIPAddress;
        private Button btnSyncTime;
        private IContainer components;
        private ToolTip deviceManagementPageToolTip;
        private Label lblFirmware;
        private Label lblHardware;
        private Label lblIP;
        private Label lblManufacturer;
        private Label lblModel;
        private Label lblSerial;
        private Label lblURL;
        private TextBox tbFirmware;
        private TextBox tbHardware;
        private TextBox tbIP;
        private TextBox tbManufacturer;
        private TextBox tbModel;
        private TextBox tbReport;
        private TextBox tbSerial;
        private TextBox tbUrl;

        public DeviceManagementPage()
        {
            this.InitializeComponent();
            this._controller = new DeviceManagementController(this);
        }

        private void btnDeviceInfo_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.GetDeviceInformation();
        }

        private void btnGetCapabilities_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.GetCapabilities();
        }

        private void btnGetHostname_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.GetHostname();
        }

        private void btnGetInterfaces_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.GetNetworkInterfaces();
        }

        private void btnGetServices_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.GetServices();
        }

        private void btnHardReset_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.HardReset();
        }

        private void btnProbe_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.GetScopes();
        }

        private void btnReboot_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.Reboot();
        }

        private void btnSetGateway_Click(object sender, EventArgs e)
        {
            TextInput input = new TextInput("Enter Network Gateway", "Gateway: ", new ValidateFunction(this.ValidateIPAddress));
            if (input.ShowDialog() == DialogResult.OK)
            {
                string gateway = input.Input;
                this.tbReport.Clear();
                this._controller.SetGateway(gateway);
            }
        }

        private void btnSetIPAddress_Click(object sender, EventArgs e)
        {
            IPInput input = new IPInput();
            if (input.ShowDialog() == DialogResult.OK)
            {
                string token = input.Token;
                string iP = input.IP;
                int prefixLength = int.Parse(input.Prefix);
                this.tbReport.Clear();
                this._controller.SetIPAddress(token, iP, prefixLength);
            }
        }

        private void btnSyncTime_Click(object sender, EventArgs e)
        {
            this.tbReport.Clear();
            this._controller.SyncTime();
        }

        public void Clear()
        {
            this.tbReport.Clear();
            this.tbFirmware.Clear();
            this.tbHardware.Clear();
            this.tbManufacturer.Clear();
            this.tbModel.Clear();
            this.tbSerial.Clear();
        }

        public void DisplayDeviceInformation(string manufacturer, string model, string firmwareVersion, string serial, string hardwareId)
        {
            base.BeginInvoke((Action)delegate
            {
                this.tbManufacturer.Text = manufacturer;
                this.tbModel.Text = model;
                this.tbFirmware.Text = firmwareVersion;
                this.tbSerial.Text = serial;
                this.tbHardware.Text = hardwareId;
            });
        }

        public void DisplayLog(string logEntry)
        {
            base.BeginInvoke(new Action(() => this.tbReport.Text = logEntry));
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void EnableFunctions(bool bEnable)
        {
            Control[] controls = new Control[] { this.btnDeviceInfo, this.btnGetHostname, this.btnGetInterfaces, this.btnHardReset, this.btnProbe, this.btnReboot, this.btnSetIPAddress, this.btnSetGateway, this.btnSyncTime, this.btnGetCapabilities, this.btnGetServices };
            if (bEnable)
            {
                base.EnableControls(controls);
            }
            else
            {
                base.DisableControls(controls);
            }
        }

        public override IController GetController()
        {
            return this._controller;
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.btnSetIPAddress = new Button();
            this.btnGetHostname = new Button();
            this.btnGetInterfaces = new Button();
            this.btnReboot = new Button();
            this.btnHardReset = new Button();
            this.btnProbe = new Button();
            this.btnDeviceInfo = new Button();
            this.lblHardware = new Label();
            this.tbHardware = new TextBox();
            this.lblFirmware = new Label();
            this.tbFirmware = new TextBox();
            this.lblSerial = new Label();
            this.tbSerial = new TextBox();
            this.lblModel = new Label();
            this.tbModel = new TextBox();
            this.lblManufacturer = new Label();
            this.tbManufacturer = new TextBox();
            this.lblIP = new Label();
            this.tbIP = new TextBox();
            this.tbReport = new TextBox();
            this.tbUrl = new TextBox();
            this.lblURL = new Label();
            this.btnSetGateway = new Button();
            this.btnSyncTime = new Button();
            this.deviceManagementPageToolTip = new ToolTip(this.components);
            this.btnGetCapabilities = new Button();
            this.btnGetServices = new Button();
            base.SuspendLayout();
            this.btnSetIPAddress.Location = new Point(0x63, 0x114);
            this.btnSetIPAddress.Name = "btnSetIPAddress";
            this.btnSetIPAddress.Size = new Size(90, 0x17);
            this.btnSetIPAddress.TabIndex = 14;
            this.btnSetIPAddress.Text = "Set IP Address";
            this.deviceManagementPageToolTip.SetToolTip(this.btnSetIPAddress, "Set IP address");
            this.btnSetIPAddress.UseVisualStyleBackColor = true;
            this.btnSetIPAddress.Click += new EventHandler(this.btnSetIPAddress_Click);
            this.btnGetHostname.Location = new Point(3, 0xf7);
            this.btnGetHostname.Name = "btnGetHostname";
            this.btnGetHostname.Size = new Size(90, 0x17);
            this.btnGetHostname.TabIndex = 13;
            this.btnGetHostname.Text = "Get Hostname";
            this.deviceManagementPageToolTip.SetToolTip(this.btnGetHostname, "Get hostname");
            this.btnGetHostname.UseVisualStyleBackColor = true;
            this.btnGetHostname.Click += new EventHandler(this.btnGetHostname_Click);
            this.btnGetInterfaces.Location = new Point(0x63, 0xf7);
            this.btnGetInterfaces.Name = "btnGetInterfaces";
            this.btnGetInterfaces.Size = new Size(90, 0x17);
            this.btnGetInterfaces.TabIndex = 12;
            this.btnGetInterfaces.Text = "Get Interfaces";
            this.deviceManagementPageToolTip.SetToolTip(this.btnGetInterfaces, "Get network interfaces");
            this.btnGetInterfaces.UseVisualStyleBackColor = true;
            this.btnGetInterfaces.Click += new EventHandler(this.btnGetInterfaces_Click);
            this.btnReboot.Location = new Point(0x63, 0xda);
            this.btnReboot.Name = "btnReboot";
            this.btnReboot.Size = new Size(90, 0x17);
            this.btnReboot.TabIndex = 11;
            this.btnReboot.Text = "Reboot";
            this.deviceManagementPageToolTip.SetToolTip(this.btnReboot, "Reboot device");
            this.btnReboot.UseVisualStyleBackColor = true;
            this.btnReboot.Click += new EventHandler(this.btnReboot_Click);
            this.btnHardReset.Location = new Point(0xc3, 0xda);
            this.btnHardReset.Name = "btnHardReset";
            this.btnHardReset.Size = new Size(90, 0x17);
            this.btnHardReset.TabIndex = 10;
            this.btnHardReset.Text = "Hard Reset";
            this.deviceManagementPageToolTip.SetToolTip(this.btnHardReset, "Set system factory defaults");
            this.btnHardReset.UseVisualStyleBackColor = true;
            this.btnHardReset.Click += new EventHandler(this.btnHardReset_Click);
            this.btnProbe.Location = new Point(3, 0xda);
            this.btnProbe.Name = "btnProbe";
            this.btnProbe.Size = new Size(90, 0x17);
            this.btnProbe.TabIndex = 9;
            this.btnProbe.Text = "Get Scopes";
            this.deviceManagementPageToolTip.SetToolTip(this.btnProbe, "Get device scopes");
            this.btnProbe.UseVisualStyleBackColor = true;
            this.btnProbe.Click += new EventHandler(this.btnProbe_Click);
            this.btnDeviceInfo.Location = new Point(3, 0xbd);
            this.btnDeviceInfo.Name = "btnDeviceInfo";
            this.btnDeviceInfo.Size = new Size(90, 0x17);
            this.btnDeviceInfo.TabIndex = 8;
            this.btnDeviceInfo.Text = "Device Info";
            this.deviceManagementPageToolTip.SetToolTip(this.btnDeviceInfo, "Get device information");
            this.btnDeviceInfo.UseVisualStyleBackColor = true;
            this.btnDeviceInfo.Click += new EventHandler(this.btnDeviceInfo_Click);
            this.lblHardware.AutoSize = true;
            this.lblHardware.Location = new Point(3, 0x89);
            this.lblHardware.Name = "lblHardware";
            this.lblHardware.Size = new Size(0x38, 13);
            this.lblHardware.TabIndex = 0x37;
            this.lblHardware.Text = "Hardware:";
            this.tbHardware.Location = new Point(0x8b, 0x86);
            this.tbHardware.Name = "tbHardware";
            this.tbHardware.ReadOnly = true;
            this.tbHardware.Size = new Size(0xe2, 20);
            this.tbHardware.TabIndex = 6;
            this.lblFirmware.AutoSize = true;
            this.lblFirmware.Location = new Point(3, 0xa3);
            this.lblFirmware.Name = "lblFirmware";
            this.lblFirmware.Size = new Size(0x34, 13);
            this.lblFirmware.TabIndex = 0x35;
            this.lblFirmware.Text = "Firmware:";
            this.tbFirmware.Location = new Point(0x8b, 160);
            this.tbFirmware.Name = "tbFirmware";
            this.tbFirmware.ReadOnly = true;
            this.tbFirmware.Size = new Size(0xe2, 20);
            this.tbFirmware.TabIndex = 7;
            this.lblSerial.AutoSize = true;
            this.lblSerial.Location = new Point(3, 0x6f);
            this.lblSerial.Name = "lblSerial";
            this.lblSerial.Size = new Size(0x4c, 13);
            this.lblSerial.TabIndex = 0x33;
            this.lblSerial.Text = "Serial Number:";
            this.tbSerial.Location = new Point(0x8b, 0x6c);
            this.tbSerial.Name = "tbSerial";
            this.tbSerial.ReadOnly = true;
            this.tbSerial.Size = new Size(0xe2, 20);
            this.tbSerial.TabIndex = 5;
            this.lblModel.AutoSize = true;
            this.lblModel.Location = new Point(3, 0x55);
            this.lblModel.Name = "lblModel";
            this.lblModel.Size = new Size(0x27, 13);
            this.lblModel.TabIndex = 0x31;
            this.lblModel.Text = "Model:";
            this.tbModel.Location = new Point(0x8b, 0x52);
            this.tbModel.Name = "tbModel";
            this.tbModel.ReadOnly = true;
            this.tbModel.Size = new Size(0xe2, 20);
            this.tbModel.TabIndex = 4;
            this.lblManufacturer.AutoSize = true;
            this.lblManufacturer.Location = new Point(3, 0x3b);
            this.lblManufacturer.Name = "lblManufacturer";
            this.lblManufacturer.Size = new Size(0x49, 13);
            this.lblManufacturer.TabIndex = 0x2f;
            this.lblManufacturer.Text = "Manufacturer:";
            this.tbManufacturer.Location = new Point(0x8b, 0x38);
            this.tbManufacturer.Name = "tbManufacturer";
            this.tbManufacturer.ReadOnly = true;
            this.tbManufacturer.Size = new Size(0xe2, 20);
            this.tbManufacturer.TabIndex = 3;
            this.lblIP.AutoSize = true;
            this.lblIP.Location = new Point(3, 0x21);
            this.lblIP.Name = "lblIP";
            this.lblIP.Size = new Size(20, 13);
            this.lblIP.TabIndex = 0x2d;
            this.lblIP.Text = "IP:";
            this.tbIP.Location = new Point(0x8b, 30);
            this.tbIP.Name = "tbIP";
            this.tbIP.ReadOnly = true;
            this.tbIP.Size = new Size(0xe2, 20);
            this.tbIP.TabIndex = 2;
            this.tbReport.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.tbReport.Location = new Point(0x173, 4);
            this.tbReport.Multiline = true;
            this.tbReport.Name = "tbReport";
            this.tbReport.ReadOnly = true;
            this.tbReport.ScrollBars = ScrollBars.Both;
            this.tbReport.Size = new Size(0x18c, 480);
            this.tbReport.TabIndex = 0x11;
            this.tbUrl.Location = new Point(0x8b, 4);
            this.tbUrl.Name = "tbUrl";
            this.tbUrl.ReadOnly = true;
            this.tbUrl.Size = new Size(0xe2, 20);
            this.tbUrl.TabIndex = 1;
            this.lblURL.AutoSize = true;
            this.lblURL.Location = new Point(3, 7);
            this.lblURL.Name = "lblURL";
            this.lblURL.Size = new Size(0x86, 13);
            this.lblURL.TabIndex = 0x29;
            this.lblURL.Text = "Device Management URL:";
            this.btnSetGateway.Location = new Point(3, 0x114);
            this.btnSetGateway.Name = "btnSetGateway";
            this.btnSetGateway.Size = new Size(90, 0x17);
            this.btnSetGateway.TabIndex = 15;
            this.btnSetGateway.Text = "Set Gateway";
            this.deviceManagementPageToolTip.SetToolTip(this.btnSetGateway, "Set default gateway");
            this.btnSetGateway.UseVisualStyleBackColor = true;
            this.btnSetGateway.Click += new EventHandler(this.btnSetGateway_Click);
            this.btnSyncTime.Location = new Point(0xc3, 0xf7);
            this.btnSyncTime.Name = "btnSyncTime";
            this.btnSyncTime.Size = new Size(90, 0x17);
            this.btnSyncTime.TabIndex = 0x10;
            this.btnSyncTime.Text = "Sync Time";
            this.deviceManagementPageToolTip.SetToolTip(this.btnSyncTime, "Set current time");
            this.btnSyncTime.UseVisualStyleBackColor = true;
            this.btnSyncTime.Click += new EventHandler(this.btnSyncTime_Click);
            this.btnGetCapabilities.Location = new Point(0x63, 0xbd);
            this.btnGetCapabilities.Name = "btnGetCapabilities";
            this.btnGetCapabilities.Size = new Size(90, 0x17);
            this.btnGetCapabilities.TabIndex = 0x38;
            this.btnGetCapabilities.Text = "Get Capabilities";
            this.btnGetCapabilities.UseVisualStyleBackColor = true;
            this.btnGetCapabilities.Click += new EventHandler(this.btnGetCapabilities_Click);
            this.btnGetServices.Location = new Point(0xc3, 0xbd);
            this.btnGetServices.Name = "btnGetServices";
            this.btnGetServices.Size = new Size(90, 0x17);
            this.btnGetServices.TabIndex = 0x39;
            this.btnGetServices.Text = "Get Services";
            this.btnGetServices.UseVisualStyleBackColor = true;
            this.btnGetServices.Click += new EventHandler(this.btnGetServices_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.btnGetServices);
            base.Controls.Add(this.btnGetCapabilities);
            base.Controls.Add(this.btnSyncTime);
            base.Controls.Add(this.btnSetGateway);
            base.Controls.Add(this.btnSetIPAddress);
            base.Controls.Add(this.btnGetHostname);
            base.Controls.Add(this.btnGetInterfaces);
            base.Controls.Add(this.btnReboot);
            base.Controls.Add(this.btnHardReset);
            base.Controls.Add(this.btnProbe);
            base.Controls.Add(this.btnDeviceInfo);
            base.Controls.Add(this.lblHardware);
            base.Controls.Add(this.tbHardware);
            base.Controls.Add(this.lblFirmware);
            base.Controls.Add(this.tbFirmware);
            base.Controls.Add(this.lblSerial);
            base.Controls.Add(this.tbSerial);
            base.Controls.Add(this.lblModel);
            base.Controls.Add(this.tbModel);
            base.Controls.Add(this.lblManufacturer);
            base.Controls.Add(this.tbManufacturer);
            base.Controls.Add(this.lblIP);
            base.Controls.Add(this.tbIP);
            base.Controls.Add(this.tbReport);
            base.Controls.Add(this.tbUrl);
            base.Controls.Add(this.lblURL);
            base.Name = "DeviceManagementPage";
            base.Size = new Size(0x307, 0x1f0);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public override void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            Action method = null;
            Action action2 = null;
            if (state.IsActive())
            {
                if (method == null)
                {
                    method = () => this.EnableFunctions(false);
                }
                base.BeginInvoke(method);
            }
            else
            {
                if (action2 == null)
                {
                    action2 = () => this.EnableFunctions(this._controller.DeviceNotEmpty() && !this._controller.RequestPending);
                }
                base.BeginInvoke(action2);
            }
        }

        private bool ValidateIPAddress(string address)
        {
            IPAddress address2;
            if (!IPAddress.TryParse(address, out address2))
            {
                this.ShowError("IP address is not correct");
                return false;
            }
            return true;
        }

        internal DeviceManagementController Controller
        {
            get
            {
                return this._controller;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string DeviceManagementUrl
        {
            get
            {
                return this.tbUrl.Text;
            }
            set
            {
                this.tbUrl.Text = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string IP
        {
            get
            {
                return this.tbIP.Text;
            }
            set
            {
                this.tbIP.Text = value;
            }
        }
    }
}

